package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.dto.BoxCreateDto;
import com.example.front.dto.BoxDto;
import com.example.front.dto.BoxSearchDto;
import com.example.front.entity.Box;
import com.example.front.entity.MessageLogging;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MessageLoggingMapper {

    @Insert("INSERT INTO `message_logging`" +
            " (`user_id`,`content`,`tenant_id`,`state`) VALUES" +
            " (#{userId},#{content},#{tenantId},#{state})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    Long create(MessageLogging box);


    @Select("<script>" +
            "SELECT * FROM `message_logging`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.userId!=null'> AND `user_id`=#{condition.userId}</if>" +
//            " AND `tenant_id`=#{condition.tenantId} " +
            " AND `status`!=" + User.STATUS_DELETE +
            "</where> " +
            " ORDER BY `id` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<MessageLogging> findAll(@Param("condition") MessageLogging condition, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT count(*) FROM `message_logging`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.userId!=null'> AND `user_id`=#{condition.userId}</if>" +
//            " AND `tenant_id`=#{condition.tenantId} " +
            " AND `status`!=" + User.STATUS_DELETE +
            "</where> " +
            "</script>")
    long count(@Param("condition") MessageLogging condition);

    @Update("UPDATE `message_logging` SET `status` =" + User.STATUS_DELETE +
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteById(long id, long tenantId);

    @Select("SELECT * FROM message_logging WHERE `tenant_id` = #{tenantId} AND `id` = #{id} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    MessageLogging findById(@Param("tenantId") long tenantId, @Param("id") long id );


}
